package com.gongJiao.controller;

import com.gongJiao.Vo.AdminVehicleVo;
import com.gongJiao.Vo.VehicleVo;
import com.gongJiao.Vo.WebVehicleInfo;
import com.gongJiao.Vo.WebVehicleSiteInfo;
import com.gongJiao.common.Result;
import com.gongJiao.entity.*;
import com.gongJiao.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * 首页控制器
 * @author Administrator
 */
@Controller
public class IndexController {
    @Autowired
    UserService userService;
    @Autowired
    InfoService infoService;
    @Autowired
    NoticeService noticeService;
    @Autowired
    VehicleService vehicleService;
    @Autowired
    SiteService siteService;

    /**
     * 首页跳转
     * @return
     */
    @GetMapping("")
    public String index(Model model) {
        //获去网站信息
        Info info = infoService.getInfo();
        //获取公告信息
        List<Notice> noticeList = noticeService.getWebNoticeList();
        //获取6条公交信息
        List<Vehicle> vehicleList=vehicleService.getSixVehicle();
        //返回网站信息
        model.addAttribute("info",info);
        model.addAttribute("noticeList",noticeList);
        model.addAttribute("vehicleList",vehicleList);
        return "index";
    }

    @GetMapping("/goUser/{id}")
    public String goUser(Model model, @PathVariable Long id){
        //根据id获取用户对象
        User user = userService.getById(id);
        model.addAttribute("user",user);
        return "userInfo";
    }

    /**
     * 根据id获取公交信息
     * @param id
     * @return
     */
    @GetMapping("/getVehicle/{id}")
    public String getVehicleById(@PathVariable Long id, Model model){
        //获取公交信息
        WebVehicleInfo webVehicleInfo=vehicleService.getwebVehicleInfoById(id);
        //获取公交中间站点信息
        WebVehicleSiteInfo webSiteInfo=vehicleService.getwebVehicleSiteInfoById(id);
        webSiteInfo.setMiddleSite(webSiteInfo.getMiddleSite().replaceAll(",","-->"));
        model.addAttribute("webVehicleInfo",webVehicleInfo);
        model.addAttribute("webSiteInfo",webSiteInfo);
        return "vehicleInfo";

    }

    /**
     * 公交信息页跳转
     * @return
     */
    @GetMapping("/vehicle")
    public String goVehicle(HttpSession session,Model model,String name,Long endSite,Long startSite) {
        //获取站点信息
        List<Site> siteList=siteService.getSiteList();
        //传递站点信息
        model.addAttribute("siteList",siteList);
        //获取公交数据
        VehicleVo vehicleVo = new VehicleVo();
        vehicleVo.setStartSite(startSite);
        vehicleVo.setEndSite(endSite);
        vehicleVo.setLimit(10000);
        vehicleVo.setPage(1);
        vehicleVo.setName(name);
        List<AdminVehicleVo> list;
        if(vehicleVo.getStartSite()==null && vehicleVo.getEndSite()==null){
            //根据名字查询以及正常查询
             list=vehicleService.getVehicleList(vehicleVo);
            model.addAttribute("WebVehicleList",list);
        }else {
            //根据路线查询
             list=vehicleService.getVehicleListByRoute(vehicleVo);

            model.addAttribute("WebVehicleList",list);
        }
        return "vehicle";
    }

    /**
     * 公交信息页跳转
     * @return
     */
    @GetMapping("/goInquireVehicle")
    @ResponseBody
    public Result goInquireVehicle(HttpSession session, String name, Long endSite, Long startSite) {
        //获取公交数据
        VehicleVo vehicleVo = new VehicleVo();
        vehicleVo.setStartSite(startSite);
        vehicleVo.setEndSite(endSite);
        vehicleVo.setLimit(10000);
        vehicleVo.setPage(1);
        vehicleVo.setName(name);
        List<AdminVehicleVo> list;
        if(vehicleVo.getStartSite()==null && vehicleVo.getEndSite()==null){
            //根据名字查询以及正常查询
            list=vehicleService.getVehicleList(vehicleVo);
            session.setAttribute("stationNum", null);
        }else {
            //根据路线查询
            list=vehicleService.getVehicleListByRoute(vehicleVo);
            if(list.size() == 1) {
                Site site = siteService.getById(startSite);
                //查询当前要乘坐的公交站
                session.setAttribute("siteName", site.getName());
                WebVehicleSiteInfo webSiteInfo=vehicleService.getwebVehicleSiteInfoById(list.get(0).getId());
                String[] split = webSiteInfo.getMiddleSite().split(",");
                int index = findIndex(split, site.getName());
                // 创建一个 Random 对象
                Random random = new Random();
                if(index != 0 && index != 1){
                    int randomNumber = random.nextInt(index);
                    int  stationNum = index - randomNumber;
                    //剩余几站
                    session.setAttribute("stationNum", stationNum);
                }else{
                    //剩余几站
                    session.setAttribute("stationNum", index);
                }


                int minute = random.nextInt(5);
                //剩余几分钟
                session.setAttribute("minute", minute == 0 ? 1 : minute);
            }

        }
        if (list == null || list.size()==0) {
            return Result.fail("暂无符合结果的公交");
        }else {
            session.setAttribute("list",list);

            return Result.success("查询成功");
        }

    }
    public static int findIndex(String[] array, String targetValue) {
        for (int i = 0; i < array.length; i++) {
            if (array[i].equals(targetValue)) {
                return i; // 返回索引值
            }
        }
        return -1; // 如果未找到目标值，则返回-1
    }
    /**
     * 公交搜索结果页面跳转
     * @return
     */
    @GetMapping("/inquireVehicle")
    public String index() {
        return "inquireVehicle";
    }
}
